/*    file: CRtpPsMediaCaster.h
 *    desc:
 *   
 * created: 2018-03-14
 *  author: chuanjiang.zh@qq.com
 * company: 
 */ 


#if !defined CRTPPSMEDIACASTER_H_
#define CRTPPSMEDIACASTER_H_

#include "PsMediaCaster.h"
#include "RtpMediaCaster.h"
#include "PSMuxer.h"
#include "H264PropDetector.h"
#include "NaluJoiner.h"
#include "TCriticalSection.h"
#include "TcpClient.h"

namespace av
{

typedef std::shared_ptr< RtpMediaCaster >	RtpMediaCasterPtr;

typedef std::shared_ptr< PSMuxer >		PsMuxerPtr;


class CRtpPsMediaCaster : public RtpPsMediaCaster, public TcpClient::Sink
{
public:
	explicit CRtpPsMediaCaster(int protocol);
	~CRtpPsMediaCaster();

	virtual int open(const std::string& ip, uint16_t port, const MediaFormat& fmt);

	virtual void close();

	virtual bool isOpen();

	virtual void setID(int id);

	virtual int getID();

	virtual bool addTarget(const std::string& ip, uint16_t port);

	virtual void removeTarget(const std::string& ip, uint16_t port);

	virtual size_t getTargetCount();

	virtual void removeAllTarget();

	virtual bool write(av::MediaPacketPtr& pkt);


	virtual void setPayload(uint8_t payload);

	virtual void setSsrc(uint32_t ssrc);


protected:
    virtual void onTcpEvent(void* obj, int event);

protected:
	bool write(int type, uint8_t* data, int size, int64_t pts, int duration, int flags);
	void writePacket(uint8_t* data, int size, int64_t pts, int flags);

protected:
	
	int	m_protocol;
	RtpMediaCasterPtr	m_caster;
	uint8_t		m_payload;
	uint32_t	m_ssrc;
	
	PsMuxerPtr	m_muxer;
	H264PropDetector	m_propDetector;
	std::shared_ptr< NaluJoiner >	m_naluJoiner;

	MediaFormat	m_format;
	comn::CriticalSection	m_cs;

	int64_t	m_videoLastPts;
	int64_t m_audioLastPts;

	int		m_id;

};


}
#endif //CRTPPSMEDIACASTER_H_

